Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | export const dynamic = "force-dynamic"; import { Metadata } from "next"; import { notFound } from "next/navigation"; import { prisma } from "@/lib/prisma"; import ShopDetailsClient from "@/components/features/shop/ShopDetailsClient"; import { JsonLd } from "@/components/shared/JsonLd"; import { generateProductMetadata, generateProductSchema, generateBreadcrumbSchema, } from "@/lib/seo"; import type { SEOProduct, BreadcrumbItem } from "@/lib/seo"; type Props = { params: Promise<{ id: string }>; }; export async function generateMetadata({ params }: Props): Promise<Metadata> { const { id } = await params; const productId = parseInt(id); // Validate that id is a valid number if (isNaN(productId)) { return { title: "Product Not Found" }; } const product = await prisma.product.findUnique({ where: { id: productId }, select: { id: true, title: true, description: true, price: true, discountedPrice: true, sku: true, stock: true, images: { select: { id: true, url: true } }, category: { select: { id: true, title: true, slug: true } }, reviews: { select: { rating: true } }, }, }); if (!product) { return { title: "Product Not Found" }; } // Transform to SEO product format const seoProduct: SEOProduct = { id: product.id, title: product.title, description: product.description, price: product.price, discountedPrice: product.discountedPrice, sku: product.sku, stock: product.stock, images: product.images.map((img) => ({ id: img.id, image: img.url })), category: product.category, reviews: product.reviews, }; return generateProductMetadata(seoProduct); } async function ProductPage({ params }: Props) { const { id } = await params; const productId = parseInt(id); if (isNaN(productId)) { notFound(); } // Fetch product and review stats in parallel for accurate data const [product, reviewStats] = await Promise.all([ prisma.product.findUnique({ where: { id: productId }, include: { images: { orderBy: { order: "asc" }}, category: true, reviews: { take: 10, include: { user: { select: { id: true, name: true, email: true } } }, orderBy: { createdAt: "desc" }}}}), // Get accurate review stats using aggregate (all reviews, not just first 10) prisma.review.aggregate({ where: { productId }, _avg: { rating: true }, _count: { rating: true }}) ]); if (!product) { notFound(); } // Extract accurate stats from aggregate query const totalReviewCount = reviewStats._count.rating; const trueAverageRating = reviewStats._avg.rating ?? 0; // Parse details from JSON string to array const parsedDetails = product.details ? (() => { try { return JSON.parse(product.details); } catch { return null; } })() : null; // Transform to frontend format const transformedProduct = { id: product.id, title: product.title, description: product.description, price: product.price, discountedPrice: product.discountedPrice, stock: product.stock, sku: product.sku, categoryId: product.categoryId, category: product.category, specifications: product.specifications, details: parsedDetails, reviews: totalReviewCount, // Use total count from aggregate, not limited array length averageRating: trueAverageRating, // Use accurate average from aggregate query imgs: { thumbnails: product.images.map((img) => img.thumbnailUrl || img.url), previews: product.images.map((img) => img.url)}, reviewsList: product.reviews, createdAt: product.createdAt, updatedAt: product.updatedAt}; // Prepare SEO data for JSON-LD const seoProduct: SEOProduct = { id: product.id, title: product.title, description: product.description, price: product.price, discountedPrice: product.discountedPrice, sku: product.sku, stock: product.stock, images: product.images.map((img) => ({ id: img.id, image: img.url })), category: product.category, reviews: product.reviews.map((r) => ({ rating: r.rating })), }; // Build breadcrumb data for JSON-LD const breadcrumbItems: BreadcrumbItem[] = [ { name: "Home", url: "/" }, { name: "Shop", url: "/shop" }, ]; if (product.category) { breadcrumbItems.push({ name: product.category.title, url: `/shop?category=${product.category.slug}`, }); } breadcrumbItems.push({ name: product.title, url: `/product/${product.id}`, }); return ( <> <JsonLd data={generateProductSchema(seoProduct)} /> <JsonLd data={generateBreadcrumbSchema(breadcrumbItems)} /> <ShopDetailsClient product={transformedProduct} /> </> ); } // Generate static params for popular products // Returns empty array if database is unavailable (e.g., in CI builds) export async function generateStaticParams() { try { const products = await prisma.product.findMany({ take: 10, select: { id: true }}); return products.map((product) => ({ id: product.id.toString()})); } catch { // Database unavailable during build (e.g., CI environment) // Return empty array to use dynamic rendering return []; } } export default ProductPage; |